package org.power.bus.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.power.bus.entity.TrainParam;
import org.power.bus.service.PowerPredictService;
import org.power.core.common.CommonResult;
import org.power.core.common.PageParamRequest;
import org.power.core.util.XlsxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.power.bus.service.PowerService;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 苗冬冬
 */
@Controller
public class HomeController {
    @Autowired
    private PowerService powerService;

    /*--------------------------------------前端页面跳转地址---------------------------------------*/
    @GetMapping("/index")
    public String index() {
        return "index";
    }

    @GetMapping("/real")
    public String ten() {
        return "Tan";
    }

    @GetMapping("/old")
    public String old() {
        return "old";
    }

    /*--------------------------------------后端接口地址---------------------------------------*/
    @PostMapping(value = "/user/login")
    @ResponseBody
    public Map login() {
        HashMap<String, Object> response = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseData.put("token", 1);
        response.put("code", 20000);
        response.put("msg", "登录成功");
        response.put("data", responseData);
        return response;
    }

    @GetMapping(value = "/user/info")
    @ResponseBody
    public Map info() {
        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseData.put("roles", "admin");
        responseData.put("name", "Super admin");
        responseData.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "登录成功");
        responseInfo.put("data", responseData);
        return responseInfo;
    }

    @ApiOperation(value = "获取机号")
    @GetMapping("/deviceNumber")
    @ResponseBody
    public Map getDeviceNumbers() {
        List list = powerService.getDeviceNumbers();

        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "获取数据成功");
        responseData.put("data", list);
        responseInfo.put("data", responseData);
        return responseInfo;
    }

    @ApiOperation(value = "获取设备名称")
    @GetMapping("/deviceName")
    @ResponseBody
    public Map getDeviceNames() {
        List list = powerService.getDeviceNames();

        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "获取数据成功");
        responseData.put("data", list);
        responseInfo.put("data", responseData);
        return responseInfo;
    }

    @ApiOperation(value = "获取模型列表")
    @GetMapping("/availableModel")
    @ResponseBody
    public Map getAvailableModel() {
        List list = powerService.getAvailableModel();

        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "获取数据成功");
        responseData.put("data", list);
        responseInfo.put("data", responseData);
        return responseInfo;
    }

    /**
     * 获取某个设备的：设备温度、环境温度、环境湿度和风速信息
     * real页面调用接口
     *
     * @param request
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "获取某个设备的信息")
    @PostMapping("/one")
    @ResponseBody
    public Map getOne(HttpServletRequest request) throws IOException {
        String deviceNum = request.getParameter("deviceNum");
        if (deviceNum == null) {
            deviceNum = "1号机";
        }
        String deviceName = request.getParameter("deviceName");
        if (deviceName == null) {
            deviceName = "1号主变35KV套管接线夹A相";
        }
        String date = request.getParameter("date");
        if (StringUtils.isNotEmpty(date)) {
            date = XlsxUtil.turnDate(date);
            System.out.println("date=>" + date);
        } else {
            date = "2021/4/1";
        }

        List list = powerService.getOneFromTemperature(deviceNum, deviceName, date);

        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "获取数据成功");
        responseData.put("data", list);
        responseInfo.put("data", responseData);
        return responseInfo;
    }

    /**
     * 读取预测数据
     * old页面调用接口
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "预测（old）")
    @PostMapping("/pre")
    @ResponseBody
    public Map getPre(HttpServletRequest request) throws Exception {
        String count = request.getParameter("count");
        if (count == null) {
            throw new RuntimeException("请传递参数count");
        }

        String gruPath = "static/data/pre/before/GRU.xlsx";
        String lstmPath = "static/data/pre/before/LSTM.xlsx";
        String truePath = "static/data/pre/before/true.xlsx";

        List list = new ArrayList();
        list.add(XlsxUtil.getPre(truePath, Integer.parseInt(count)));
        list.add(XlsxUtil.getPre(lstmPath, Integer.parseInt(count)));
        list.add(XlsxUtil.getPre(gruPath, Integer.parseInt(count)));

        List timeList = new ArrayList();
        int num = Integer.parseInt(count);
        for (int i = 1; i <= 24; i++) {
            timeList.add(getTime(num + i));
        }
        System.out.println();
        list.add(timeList);

        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "获取数据成功");
        responseData.put("data", list);
        responseInfo.put("data", responseData);
        return responseInfo;
    }

    /**
     * 读取预测excel表格
     * index页面调用接口
     *
     * @param request
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "预测（index）")
    @PostMapping("/pre2")
    @ResponseBody
    public Map getPre2(HttpServletRequest request) throws Exception {
        String count = request.getParameter("count");
        if (count == null) {
            throw new RuntimeException("请传递参数count");
        }
        String truePath = "static/data/预测结果/TRUE.xlsx";
        String gruPath = "static/data/预测结果/GRU.xlsx";
        String lstmPath = "static/data/预测结果/LSTM.xlsx";

        //String gruPath=pathResource+"/预测结果/GRU.xlsx";
        //String lstmPath=pathResource+"/预测结果/LSTM.xlsx";
        //String truePath=pathResource+"/预测结果/TRUE.xlsx";

        List list = new ArrayList();
        list.add(XlsxUtil.getPre2(truePath, Integer.parseInt(count)));
        list.add(XlsxUtil.getPre2(lstmPath, Integer.parseInt(count)));
        list.add(XlsxUtil.getPre2(gruPath, Integer.parseInt(count)));

        List timeList = new ArrayList();
        int num = Integer.parseInt(count);
        for (int i = 1; i <= 24; i++) {
            timeList.add(getTime(num + i));
        }
        System.out.println();
        list.add(timeList);

        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "获取数据成功");
        responseData.put("data", list);
        responseInfo.put("data", responseData);

        return responseInfo;
    }

    public String getTime(int count) {
        int day = count / (2 * 24) + 1;
        int hour = (count % (2 * 24)) / 2;
        int second = count % 2;
        System.out.print(day + ":" + hour + ":" + second * 30 + "  ");
        return "9/" + day + " " + hour + ":" + second * 30;
    }

    @GetMapping("/testFile")
    @ResponseBody
    public String test() {
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/data/预测结果/TRUE.xlsx");
        if (inputStream == null) {
            System.out.println("inputStream==null");
        }
        return "Yes";
    }

    /**
     * 上传温度excel表接口
     *
     * @param multipart
     * @return
     */
    @ApiOperation(value = "上传温度excel表接口")
    @PostMapping("/uploadTemperatureExcel")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "温度表excel生成日期", required = false)
    })
    public Map uploadTemperatureExcel(@RequestParam("temperaturefile") MultipartFile multipart, @RequestParam("date") String date) throws Exception {
        boolean rst = false;
        rst = powerService.uploadTemperatureExcel(multipart, date);

        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "上传数据成功");
        responseData.put("data", rst);
        responseInfo.put("data", responseData);
        return responseInfo;
    }

    /**
     * 上传电流excel表接口
     *
     * @param multipart
     * @return
     */
    @ApiOperation(value = "上传电流excel表接口")
    @PostMapping("/uploadCurrentExcel")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date", value = "日期", required = false)
    })
    public Map uploadCurrentExcel(@RequestParam("currentfile") MultipartFile multipart, @RequestParam("date") String date) throws Exception {
        boolean rst = false;
        rst = powerService.uploadCurrentExcel(multipart, date);

        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "上传数据成功");
        responseData.put("data", rst);
        responseInfo.put("data", responseData);
        return responseInfo;
    }

    /**
     * 查询设备列表
     *
     * @return
     */
    @ApiOperation(value = "查询设备列表")
    @GetMapping("/equipment_list")
    @ResponseBody
    public CommonResult listEquipment(PageParamRequest pageParamRequest) {
        return CommonResult.success(powerService.listEquipment(pageParamRequest));
    }

    /**
     * 添加设备
     *
     * @return
     */
    @ApiOperation(value = "添加设备")
    @PostMapping("/equipment_add")
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "equipment", value = "设备名称", required = true)
    })
    public CommonResult addEquipment(@RequestParam("equipment") String equipment) {
        boolean result = powerService.addEquipment(equipment);
        if (result) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation("合并为特征数据")
    @GetMapping("/feature_merge")
    @ResponseBody
    public CommonResult mergeFeature() {
        boolean result = powerService.mergeFeature();
        if (result) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    /**
     * 模型训练
     *
     * @param trainParam
     * @return
     */
    @ApiOperation("模型训练")
    @PostMapping("/trainModel")
    @ResponseBody
    public Map trainModel(@RequestBody TrainParam trainParam) {
        boolean result = powerService.trainModel(trainParam);

        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "获取数据成功");
        responseData.put("data", result);
        responseInfo.put("data", responseData);

        return responseInfo;
    }

    @Autowired
    private PowerPredictService powerPredictService;
    @ApiOperation("模型预测")
    @GetMapping("/predictModel")
    @ResponseBody
    public Map predictModel(@RequestParam("usingModel") String usingModel) {
        List result = powerPredictService.predictModel(usingModel);

        HashMap<String, Object> responseInfo = new HashMap<>();
        HashMap<String, Object> responseData = new HashMap<>();
        responseInfo.put("code", 20000);
        responseInfo.put("msg", "获取数据成功");
        responseData.put("data", result);
        responseInfo.put("data", responseData);

        return responseInfo;
    }
}
